package com.example.gslih.componentproject.main.slideconflict;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;

import java.util.Collections;
import java.util.List;

import static android.content.ContentValues.TAG;

public class SimpleItemTouchCallback extends ItemTouchHelper.Callback {
    private SlidingConflictActivity.RvAdapterWrapper mAdapter;
    private SlidingConflictActivity.RvAdapter mAdapter2;
    private List<String> mData;
    public SimpleItemTouchCallback(SlidingConflictActivity.RvAdapterWrapper adapter){
        mAdapter = adapter;
        mData = adapter.getDatas();
    }

    public SimpleItemTouchCallback(SlidingConflictActivity.RvAdapter adapter){
        mAdapter2 = adapter;
        mData = mAdapter2.datas;
    }

    //设置支持的拖拽、滑动的方向
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END; //s上下拖拽
        int swipeFlag = ItemTouchHelper.START | ItemTouchHelper.END; //左->右和右->左滑动
        return makeMovementFlags(dragFlag,0);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        int from = viewHolder.getAdapterPosition();
        int to = target.getAdapterPosition();
        Collections.swap(mData, from, to);
        mAdapter.notifyItemMoved(from, to);
//        mAdapter2.notifyItemMoved(from, to);
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        int pos = viewHolder.getAdapterPosition();
        Log.d(TAG, "onSwiped: "+pos);
        mData.remove(pos-1);
        mAdapter.notifyItemRemoved(pos);
//        mAdapter2.notifyItemRemoved(pos);
    }

    //状态改变时回调
    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
        if(actionState != ItemTouchHelper.ACTION_STATE_IDLE){
            SlidingConflictActivity.RvAdapter.ViewHolder holder = (SlidingConflictActivity.RvAdapter.ViewHolder)viewHolder;
            holder.itemView.setBackgroundColor(0xa0f17c14); //设置拖拽和侧滑时的背景色
        }
    }

    //拖拽或滑动完成之后调用，用来清除一些状态
    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        SlidingConflictActivity.RvAdapter.ViewHolder holder = (SlidingConflictActivity.RvAdapter.ViewHolder)viewHolder;
        holder.itemView.setBackgroundColor(0xffeeeeee); //背景色还原
    }
}